﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace nExt.Core.Helper
{
    /// <summary>
    /// Extensions for an enum
    /// </summary>
    public static class EnumExtensions
    {
		/// <summary>
		/// Konvertieren eines Enums in ein Dictionary
		/// </summary>
		/// <typeparam name="TEnum">The type of the enum.</typeparam>
		public static IDictionary<string, object> ToDictionary<TEnum>()
		{
			return Enum.GetValues(typeof(TEnum))
				.Cast<TEnum>()
				.ToDictionary(enumValue => enumValue.ToString(), enumValue => (object)Convert.ToInt32(enumValue));
		}

        /// <summary>
		/// GetAttributes on enum
        /// </summary>
        public static T[] GetAttributes<T>(this Enum enumeration) where T : Attribute
        {
            if (enumeration == null)
                return new T[0];

            var fieldInfo = enumeration.GetType().GetField(enumeration.ToString());
            return fieldInfo != null ? (T[])fieldInfo.GetCustomAttributes(typeof(T), false) : new T[0];
        }
    }
}
